//
//  ViewController.swift
//  JKGPUImage
//
//  Created by 王冲 on 2020/8/4.
//  Copyright © 2020 王冲. All rights reserved.
//

import UIKit
import GPUImage
class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    lazy var image: UIImage? = UIImage(named: "小可爱")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    
    /// 卡通
    @IBAction func katong(_ sender: Any) {
        
        let caisemohu = GPUImageSepiaFilter()
        imageView.image = processImage(filter: caisemohu)
        
    }
    
    /// 彩色模糊
    @IBAction func caisemohu(_ sender: Any) {
        
        let caisemohu = GPUImageLevelsFilter()
        imageView.image = processImage(filter: caisemohu)
    }
    
    /// 水晶球
    @IBAction func shuijingqiu(_ sender: Any) {
        let shuijingqiu = GPUImageGlassSphereFilter()
        imageView.image = processImage(filter: shuijingqiu)
    }
    
    
    /// 滤镜处理图片
    func processImage(filter: GPUImageFilter) -> UIImage? {
        // 1.创建GPUImagePicture处理图片
        let picProgress = GPUImagePicture(image: image)
        // 2.添加需要处理的滤镜
        picProgress?.addTarget(filter)
        // 3.处理图片
        filter.useNextFrameForImageCapture()
        picProgress?.processImage()
        // 4.取出最新的图片
        return filter.imageFromCurrentFramebuffer()
        
    }
    /// 模糊效果
    func testMohu() {
        // 1.获取修改的图片
        guard let sourceImage = UIImage(named: "小可爱") else {
            return
        }
        
        // 2.使用GPUImage高斯模糊效果
        // 2.1、如果是对图像进行处理 GPUImagePicture
        let picProcess = GPUImagePicture(image: sourceImage)
        // 2.2、添加需要处理的滤镜
        let blurFilter = GPUImageGaussianBlurFilter()
        // 纹理
        blurFilter.texelSpacingMultiplier = 5
        // 对每个像素点处理
        blurFilter.blurRadiusInPixels = 5
        picProcess?.addTarget(blurFilter)
        // 2.3.处理图片
        blurFilter.useNextFrameForImageCapture()
        picProcess?.processImage()
        // 2.4、取出最新的图片
        let newImage = blurFilter.imageFromCurrentFramebuffer()
        
        // 3.显示最新的图片
        imageView.image = newImage
    }


}

